//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package com.atjg.autocat.uvccamera.utils;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.graphics.Rect;
import android.graphics.YuvImage;
import java.io.ByteArrayOutputStream;

public class ImageUtil {
    public ImageUtil() {
    }

    public static Bitmap yuv2Bitmap(byte[] yuv, int width, int height) {
        Bitmap bitmap = null;

        try {
            ByteArrayOutputStream bos = new ByteArrayOutputStream(yuv.length);
            YuvImage yuvImage = new YuvImage(yuv, 17, width, height, (int[])null);
            boolean success = yuvImage.compressToJpeg(new Rect(0, 0, width, height), 100, bos);
            if (success) {
                byte[] buffer = bos.toByteArray();
                bitmap = BitmapFactory.decodeByteArray(buffer, 0, buffer.length);
            }

            bos.flush();
            bos.close();
        } catch (Exception var8) {
            Exception e = var8;
            e.printStackTrace();
        }

        return bitmap;
    }

    public static Bitmap yuv2Bitmap(byte[] yuv, int width, int height, float rotation) {
        Bitmap bitmap = yuv2Bitmap(yuv, width, height);
        return rotateBimap(bitmap, rotation);
    }

    public static Bitmap rotateBimap(Bitmap bitmap, float rotation) {
        if (bitmap != null && rotation != 0.0F) {
            Matrix matrix = new Matrix();
            matrix.postRotate(rotation);
            Bitmap rotateBimap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
            if (bitmap != null && !bitmap.isRecycled()) {
                bitmap.recycle();
                bitmap = null;
            }

            return rotateBimap;
        } else {
            return bitmap;
        }
    }

    public static Bitmap scaleBitmap(Bitmap bitmap, int newWidth, int newHeight) {
        if (bitmap == null) {
            return null;
        } else {
            int width = bitmap.getWidth();
            int height = bitmap.getHeight();
            float scaleWidth = (float)newWidth / (float)width;
            float scaleHeight = (float)newHeight / (float)height;
            Matrix matrix = new Matrix();
            matrix.postScale(scaleWidth, scaleHeight);
            Bitmap scaleBitmap = null;

            try {
                scaleBitmap = Bitmap.createBitmap(bitmap, 0, 0, width, height, matrix, true);
            } catch (OutOfMemoryError var10) {
                while(scaleBitmap == null) {
                    System.gc();
                    System.runFinalization();
                    scaleBitmap = Bitmap.createBitmap(bitmap, 0, 0, width, height, matrix, true);
                }
            }

            if (bitmap != scaleBitmap && !bitmap.isRecycled()) {
                bitmap.recycle();
                bitmap = null;
            }

            return scaleBitmap;
        }
    }
}
